package service

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat_1004/global"
	"wechat_1004/internal/model"
	"wechat_1004/internal/params"
)

func AddContact(c *gin.Context) {
	var req params.AddContactForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var friend model.Friend
	friend = model.Friend{
		UserId:   req.UserId,
		FriendId: req.FriendId,
		Name:     req.Name,
		Status:   1,
	}

	err := friend.AddFriend()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "添加失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "添加成功",
		"data":    friend.Id,
	})

}

func FindListContact(c *gin.Context) {
	var req params.FindListContactForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var friend model.Friend

	friend.UserId = req.UserId
	findFriend, err := friend.FindFriend()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查询失败",
			"data":    err.Error(),
		})
		return
	}

	global.RDB.Set(context.Background(), "ListFriend", findFriend, 0)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查询成功",
		"data":    findFriend,
	})
}
